//
//  NSString+TYZAdd.m
//  DrivieSchoolStudents
//
//  Created by TangBin on 2019/6/28.
//  Copyright © 2019 班文政. All rights reserved.
//

#import "NSString+TYZAdd.h"

@implementation NSString (TYZAdd)
/**
 得到字符串的长度，把表情作为一个字（一位）eg:@"内衣👙额" 一个字占一个位置，而表情可能占两个、三个位置。
 
 @return return value description
 */
- (NSUInteger)cLength
{
    __block NSUInteger len = 0;
    [self enumerateSubstringsInRange:NSMakeRange(0, self.length) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString * _Nullable substring, NSRange substringRange, NSRange enclosingRange, BOOL * _Nonnull stop) {
        len++;
    }];
    return len;
}

/**
 字符串是不是数字
 
 @return 是数字返回YES；否则返回NO
 */
- (BOOL)isAllNum
{
    unichar c;
    for (int i=0; i<self.length; i++)
    {
        c = [self characterAtIndex:i];
        if (!isdigit(c))
        {
            return NO;
        }
    }
    return YES;
}

/// 判断是不是英文字母
- (BOOL)isAlpha
{
    unichar c;
    for (int i=0; i<self.length; i++)
    {
        c = [self characterAtIndex:i];
        if (!isalpha(c))
        {
            return NO;
        }
    }
    return YES;
}

/**
 做编码
 
 @return return value description
 */
- (NSString *)addingPercentEncode
{
    return [self stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
}

- (NSString *)removingPercentEncode
{
    return [self stringByRemovingPercentEncoding];
}

/// 得到行数
/// @param font 字大小
/// @param width 宽度
- (NSInteger)lineRow:(UIFont *)font width:(CGFloat)width
{
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:self];
    YYTextContainer *container = [YYTextContainer new];
    container.size = CGSizeMake(width, HUGE);
    YYTextLayout *contentLayout = [YYTextLayout layoutWithContainer:container text:text];
    if (contentLayout)
    {
        return contentLayout.rowCount;
    }
    return 1;
}

- (NSString *)transformToPinyin {
    if ([self isEqualToString:@""]) {
        return @"";
    }
    
    NSMutableString *mutableString = [NSMutableString stringWithString:self];
    CFStringTransform((CFMutableStringRef)mutableString, NULL, kCFStringTransformToLatin, false);
    mutableString = (NSMutableString *)[mutableString stringByFoldingWithOptions:NSDiacriticInsensitiveSearch locale:[NSLocale currentLocale]];
    NSArray *arr = [mutableString componentsSeparatedByString:@" "];
    return [arr componentsJoinedByString:@""];
}


@end
